<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Create HTML-Block to display on productdetailpage
 * used by article.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */

/**
 * create the options-block for the product-attribute
 * 
 * used by price.calculate.php
 */
class optionsShow
{

	// @var $_id String attribute-id
	private $_html = '';

	/**
	 * Initializes the system-variable if still not available. 
	 * @param  string $attribute_id   product-id
	 */
	function __construct($attribute_id)
	{
		if ($attribute_id <= 0)
			return '';

		// Get all options for given attribute-id
		db::getInstance()->where('options_attribute_id', $attribute_id);
		db::getInstance()->orderBy('options_order', 'ASC');
		$optionsList = db::getInstance()->get('options');

		foreach ($optionsList as $optionRow)
		{
			switch ($optionRow['options_art'])
			{
				case '1': // Textrow
					$this->_getTextrow($optionRow);
					break;
				case '2': // Textfield
					$this->_getTextfield($optionRow);
					break;
				case '3': // Drop-Down
					$this->_getDropDown($optionRow);
					break;
				case '4': // Option-List
					$this->_getOptionList($optionRow);
					break;
			}
		}
		return;
	}

	/**
	 * create the html-code for one textrow
	 * @param  string $optionRow   one databserow
	 */
	private function _getTextrow($optionRow)
	{
		$rowHtml = _('No Textrowtemplate found!');
		$file = 'template/option.form/option.textrow.tpl.php';
		if (is_file($file))
		{
			$rowHtml = file_get_contents($file);
			$parts = explode('?>', $rowHtml);
			$rowHtml = $parts[1];

			// create html
			$textrowx = '<input name="' . $optionRow['options_id'] . '_' . '" type="text" ' .
				'size="' . $optionRow['options_width'] . '" value="' . $optionRow['options_text'] . '">';
			// include html in template
			$search = array('{textrowtitel}', '{textrowx}');
			$value = array($optionRow['options_titel'], $textrowx);
			$this->_html .= str_replace($search, $value, $rowHtml);
			return;
		}
	}

	/**
	 * create the html-code for one textfield
	 * @param  string $optionRow   one databserow
	 */
	private function _getTextfield($optionRow)
	{
		$rowHtml = _('No Textrowtemplate found!');
		$file = 'template/option.form/option.textfield.tpl.php';
		if (is_file($file))
		{
			$rowHtml = file_get_contents($file);
			$parts = explode('?>', $rowHtml);
			$rowHtml = $parts[1];

			// create html
			$textfieldx = '<textarea name="' . $optionRow['options_id'] . '_' . '" ' .
				'cols="' . $optionRow['options_width'] . '" rows="' . $optionRow['options_height'] . '">' .
				$optionRow['options_text'] . '</textarea>';
			// include html in template
			$search = array('{textfieldtitel}', '{textfieldx}');
			$value = array($optionRow['options_titel'], $textfieldx);
			$this->_html .= str_replace($search, $value, $rowHtml);
			return;
		}
	}

	/**
	 * create the html-code for one drop-downmenue
	 * @param  string $optionRow   one databserow
	 */
	private function _getDropDown($optionRow)
	{
		$rowHtml = _('No Textrowtemplate found!');
		$file = 'template/option.form/option.dropdown.tpl.php';
		if (is_file($file))
		{
			$rowHtml = file_get_contents($file);
			$parts = explode('?>', $rowHtml);
			$rowHtml = $parts[1];

			// create html
			$dropdownx = '<select name="' . $optionRow['options_id'] . '_' . '" size="1">';

			$rows = explode("\n", $optionRow['options_text']);
			foreach ($rows as $row)
			{
				$part = explode(':', $row);
				$dropdownx .= '<option value="' . $part[0] . '|' . trim($part[1]) . '">' .
					$part[0] . '&nbsp&nbsp(' . trim($part[1]) . CURRENCY . ')</option>';
			}
			$dropdownx .= '</select>';

			// include html in template
			$search = array('{dropdowntitel}', '{dropdownx}');
			$value = array($optionRow['options_titel'], $dropdownx);
			$this->_html .= str_replace($search, $value, $rowHtml);
			return;
		}
	}

	/**
	 * create the html-code for one optionslist
	 * @param  string $optionRow   one databserow
	 */
	private function _getOptionList($optionRow)
	{
		$rowHtml = _('No Textrowtemplate found!');
		$file = 'template/option.form/option.option.tpl.php';
		if (is_file($file))
		{
			$rowHtml = file_get_contents($file);
			$parts = explode('?>', $rowHtml);
			$rowHtml = $parts[1];

			// create html
			$optionx = '';
			$rows = explode("\n", $optionRow['options_text']);
			foreach ($rows as $row)
			{
				$part = explode(':', $row);
				$optionx .= '<input type="checkbox" name="' . $optionRow['options_id'] . '_[]' .
					'" value="' . $part[0] . '|' . trim($part[1]) . '"> ' . $part[0] .
					'&nbsp&nbsp(' . trim($part[1]) . CURRENCY . ')<br>';
			}

			// include html in template
			$search = array('{optiontitel}', '{optionx}');
			$value = array($optionRow['options_titel'], $optionx);
			$this->_html .= str_replace($search, $value, $rowHtml);
			return;
		}
	}

	/**
	 * returns the html-code for all options
	 * @return string	html-code
	 */
	public function getOptionHtml()
	{
		$optionsHtml = _('No Optionstemplate found!');
		$file = 'template/option.form/options.tpl.php';
		if (is_file($file))
		{
			$optionsHtml = file_get_contents($file);
			$parts = explode('?>', $optionsHtml);
			$optionsHtml = $parts[1];
			return str_replace('{optionshtml}', $this->_html, $optionsHtml);
		}
	}

}

?>